package com.ocelot.san.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.lang.StringUtils;

import com.ocelot.san.dto.OperationType;
import com.ocelot.san.form.OperationTypeForm;
import com.ocelot.san.service.OperationTypeService;


@ManagedBean(name = "operationTypeController")
@ViewScoped
public class OperationTypeController {

	private OperationTypeForm operationTypeForm;
	
	@ManagedProperty(value = "#{operationTypeService}")
	private OperationTypeService operationTypeService;
	
	@PostConstruct
	public void init() {
		operationTypeForm = new OperationTypeForm();
		operationTypeForm.setEditOperationType(new OperationType());
		refreshOperationTypeList();
	}
	
	
	public void submitOperationType(ActionEvent event) {
		if (validateForm()) {
			operationTypeService.save(operationTypeForm.getEditOperationType());
			// clear Data
			operationTypeForm.setEditOperationType(new OperationType());
			refreshOperationTypeList();
		}
	}
	
	

	protected void refreshOperationTypeList() {
		operationTypeForm.setOperationTypes(operationTypeService.fetchAll());
	}
	
	public boolean validateForm() {
		List<String> errors = new ArrayList<String>();
		
		if(StringUtils.isEmpty(operationTypeForm.getEditOperationType().getDescription())) {
			errors.add("La Descripción es requerida");
		}
		
		if(errors.size() > 0) {
			for(String error:errors){
				 FacesMessage message=new FacesMessage(error);  
				 message.setSeverity(FacesMessage.SEVERITY_INFO);  
				 FacesContext ctx= FacesContext.getCurrentInstance();  
				 ctx.addMessage("operationTypeForm", message);
			}
			return false;
		}
		return true;
	}

	
	public String editItem() {
		Long idToEdit = operationTypeForm.getToEdit();
		OperationType toModify = getOperationTypeById(idToEdit);
		operationTypeForm.setEditOperationType(toModify);
		return null;
	}

	public String removeItem() {
		Long indexToRemove = operationTypeForm.getToRemove();
		OperationType removeItem = getOperationTypeById(indexToRemove);
		if (removeItem != null) {
			operationTypeService.delete(removeItem.getId());
			operationTypeForm.getOperationTypes().remove(removeItem);
			operationTypeForm.setEditOperationType(new OperationType());
			refreshOperationTypeList();
		}
		return null;
	}
	
	protected OperationType getOperationTypeById(Long id){
		return operationTypeService.findById(id);
	}


	public OperationTypeForm getOperationTypeForm() {
		return operationTypeForm;
	}


	public void setOperationTypeForm(OperationTypeForm operationTypeForm) {
		this.operationTypeForm = operationTypeForm;
	}


	public OperationTypeService getOperationTypeService() {
		return operationTypeService;
	}


	public void setOperationTypeService(OperationTypeService operationTypeService) {
		this.operationTypeService = operationTypeService;
	}

	
}
